package com.testmanager.testmananger.practiseDemo.taskWork;


import com.alibaba.fastjson.JSONObject;
import com.testmanager.testmananger.foundation.security.SpringSecurityAuditorAware;
import com.testmanager.testmananger.foundation.util.FileUtils;
import com.testmanager.testmananger.shuiyou.szjk.repository.ChangeRecordRepository;
import com.testmanager.testmananger.shuiyou.szjk.service.SendZipService;
import com.testmanager.testmananger.shuiyou.szjk.service.UnzipUploadService;
import com.testmanager.testmananger.shuiyou.szjk.service.ZipAndSendService;
import com.testmanager.testmananger.shuiyou.szjk.service.ZipTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Optional;

/**
 * 1.接口关联  登录 + 新增 + 查询 + 删除
 * 2.get清求
 * 3.rest get请求
 * 4.post请求
 * 5.上传接口 + rest
 * 6.下载接口
 */
@Slf4j
@RestController
@RequestMapping("work")
public class JobResource {

    @Autowired
    UnzipUploadService unzipUploadService;

    @Autowired
    ZipAndSendService zipAndSendService;

    @Autowired
    ZipTypeService zipTypeService;

    @Autowired
    SendZipService sendZipService;

    @Autowired
    ChangeRecordRepository changeRecordRepository;

    @Autowired
    SpringSecurityAuditorAware springSecurityAuditorAware;

    @PostMapping("/upload")
    public ResponseEntity upload(@RequestParam("type")long typeid, @RequestParam(value = "file",required = false) MultipartFile file){


        if (typeid != 1){
            return  ResponseEntity.status(403).body("typeid 错误");
        }




        return upload1(typeid,file);

    }




    public ResponseEntity upload1(long typeid,MultipartFile file){


        String fileName = file.getOriginalFilename();

        String path = "";


        String truefileNmae = fileName;
        path = FileUtils.BASEPATH + truefileNmae;

        File dest = new File(path);

        if (dest.getParentFile() != null && !dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);

        } catch (Exception e) {

            e.printStackTrace();
            return ResponseEntity.status(500).body(e.getMessage());
        }

        return  ResponseEntity.ok("成功");


    }


    @GetMapping("/download/{filename}")
    public void download(HttpServletRequest req, HttpServletResponse rep,@PathVariable("filename")String filename) throws  Exception{

        URL url = new URL("http://wwww.baidu.com");
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();

     //   rep.reset();  //这里会清空全局配置  导致存在跨域问题
        rep.setContentType(conn.getContentType());

        rep.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("downloaddemo", "UTF-8"));


        byte[] buffer = new byte[1024];
        int len;
        OutputStream outputStream = rep.getOutputStream();
        while ((len = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        inputStream.close();
    }




    public  String test(){
        String str = "dfdfdf";

        ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
        return this.getClass().getResource("/").getPath();

    }



    @GetMapping("get")
    public ResponseEntity get(@RequestParam(value="name")String name,@RequestParam(value = "hobby")String job){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",name);
        jsonObject.put("hobby",job);
        jsonObject.put("operator",springSecurityAuditorAware.getCurrentAuditor().get());
        if (null == name || null == job){
            return ResponseEntity.status(500).body("没有输入name 或者没有输入job");
        }

        return ResponseEntity.ok().body(jsonObject);
    }

    @GetMapping("restget/{name}/{hobby}")
    public ResponseEntity getrest(@PathVariable(value="name")String name,@PathVariable(value = "hobby")String job){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",name);
        jsonObject.put("hobby",job);
        jsonObject.put("operator",springSecurityAuditorAware.getCurrentAuditor().get());
        if (null == name || null == job){
            return ResponseEntity.status(500).body("没有输入name 或者没有输入job");
        }

        return ResponseEntity.ok().body(jsonObject);
    }


    @PostMapping("/post")
    public ResponseEntity post(@RequestBody JSONObject jsonObject){
        jsonObject.put("operator",springSecurityAuditorAware.getCurrentAuditor().get());
        return ResponseEntity.ok().body(jsonObject);
    }





}
